﻿CREATE PROCEDURE temp.s_WaterFall
	@Payment money,
	@NewPayment money,
	@ToInterest money,
	@LateFee money,
	@NSF money,
	@EscrowInterest money,
	@EscrowLateFee money,
	@EscrowNSF money
AS
DECLARE @TranBalance money SET @TranBalance = @NewPayment
DECLARE @ToPrincipal money SET @ToPrincipal = dbo.MaxNum(0,@Payment - @ToInterest)

DECLARE @IntEscPart money SET @IntEscPart = dbo.WholeOrLess(@TranBalance,-@EscrowInterest)
SET @TranBalance = @TranBalance - @IntEscPart

DECLARE @InterestPart money SET @InterestPart = dbo.WholeOrLess(@TranBalance,@ToInterest)
SET @TranBalance = @TranBalance - @InterestPart

DECLARE @PrincipalPart money SET @PrincipalPart = dbo.WholeOrLess(@TranBalance,@ToPrincipal)
SET @TranBalance = @TranBalance - @PrincipalPart

DECLARE @LateEscPart money SET @LateEscPart = dbo.WholeOrLess(@TranBalance,-@EscrowLateFee)
SET @TranBalance = @TranBalance - @LateEscPart

DECLARE @LateFeePart money SET @LateFeePart = dbo.WholeOrLess(@TranBalance,@LateFee)
SET @TranBalance = @TranBalance - @LateFeePart

DECLARE @NSFEscPart money SET @NSFEscPart = dbo.WholeOrLess(@TranBalance,-@EscrowNSF)
SET @TranBalance = @TranBalance - @NSFEscPart

DECLARE @NSFPart money SET @NSFPart = dbo.WholeOrLess(@TranBalance,@NSF)
SET @TranBalance = @TranBalance - @NSFPart

-------------------------------------------------------------
SELECT @ToPrincipal ToPrincipal,@IntEscPart IntEscPart,@InterestPart InterestPart,@PrincipalPart PrincipalPart,
@LateEscPart LateEscPart,@LateFeePart LateFeePart,@NSFEscPart NSFEscPart,@NSFPart NSFPart,
@TranBalance TranBalance


